package com.ruoyi.web.controller.emergencyplan;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.pengjie.SnowflakeIdUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.emergencyplan.domain.MaterialCategory;
import com.ruoyi.emergencyplan.dto.MaterialCategoryDto;
import com.ruoyi.emergencyplan.service.IMaterialCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 物资类别Controller
 * 
 * @author hcc
 * @date 2023-12-17
 */
@RestController
@RequestMapping("/emergencyplan/category")
public class MaterialCategoryController extends BaseController
{
    @Autowired
    private IMaterialCategoryService materialCategoryService;

    /**
     * 查询物资类别列表
     */
    @PreAuthorize("@ss.hasPermi('emergencyplan:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(MaterialCategory materialCategory)
    {
        startPage();
        List<MaterialCategory> list = materialCategoryService.selectMaterialCategoryList(materialCategory);
        return getDataTable(list);
    }

    /**
     * 导出物资类别列表
     */
    @PreAuthorize("@ss.hasPermi('emergencyplan:category:export')")
    @Log(title = "物资类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaterialCategory materialCategory)
    {
        List<MaterialCategory> list = materialCategoryService.selectMaterialCategoryList(materialCategory);
        ExcelUtil<MaterialCategory> util = new ExcelUtil<MaterialCategory>(MaterialCategory.class);
        util.exportExcel(response, list, "物资类别数据");
    }

    /**
     * 获取物资类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('emergencyplan:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(materialCategoryService.selectMaterialCategoryById(id));
    }

    /**
     * 新增物资类别
     */
    @PreAuthorize("@ss.hasPermi('emergencyplan:category:add')")
    @Log(title = "物资类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MaterialCategory materialCategory)
    {
        materialCategory.setId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
        return toAjax(materialCategoryService.insertMaterialCategory(materialCategory));
    }

    /**
     * 修改物资类别
     */
    @PreAuthorize("@ss.hasPermi('emergencyplan:category:edit')")
    @Log(title = "物资类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MaterialCategory materialCategory)
    {
        return toAjax(materialCategoryService.updateMaterialCategory(materialCategory));
    }

    /**
     * 删除物资类别
     */
    @PreAuthorize("@ss.hasPermi('emergencyplan:category:remove')")
    @Log(title = "物资类别", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(materialCategoryService.deleteMaterialCategoryByIds(ids));
    }
    /**
     * 查询物资类别列表
     */
    @PreAuthorize("@ss.hasPermi('emergencyplan:category:list')")
    @GetMapping("/treeList")
    public TableDataInfo treeList(MaterialCategory materialCategory) {
        startPage();
        List<MaterialCategoryDto> list = materialCategoryService.getMaterialCategoryList(materialCategory);
        return getDataTable(list);
    }
}
